package helper

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"rongyy/src/middlewares"
)

type RespWxSession struct {
	Errmsg     string `json:"errmsg"`
	SessionKey string `json:"session_key"`
	Openid     string `json:"openid"`
	Unionid    string `json:"unionid"`
}

type ReqWxPhone struct {
	Code          string `json:"code"`
	Iv            string `json:"iv"`
	EncryptedData string `json:"encrypted_data"`
}

type RespWxPhone struct {
	PhoneNumber     string `json:"phoneNumber"`
	PurePhoneNumber string `json:"purePhoneNumber"`
}

type RespWxApp struct {
	Errmsg      string `json:"errmsg"`
	Errcode     int64  `json:"errcode"`
	AccessToken string `json:"access_token"`
	ExpriesIn   int64  `json:"expries_in"`
	Unionid     string `json:"unionid"`
	Openid      string `json:"openid"`
	Nickname    string `json:"nickname"`
	Sex         int    `json:"sex"`
	Language    string `json:"language"`
	City        string `json:"city"`
	Province    string `json:"province"`
	Country     string `json:"country"`
	Headimgurl  string `json:"headimgurl"`
}

//
//{"errcode":40163,"errmsg":"code been used, hints: [ req_id: pfNafl.LRa-h.G_3a ]"}
//{
//"errcode": 40029,
//"errmsg": "invalid code rid: 60123008-08f09ac5-7e6b6f15"
//}

func AppGetUserInfo(code string) (*RespWxApp, error) {
	uri := "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxd5cd38899ac167af&secret=2822442afbb71fab05f6fb7a8b43f442&code=" + code + "&grant_type=authorization_code"
	resp, err := http.Get(uri)
	if err != nil {
		fmt.Println("这里4=====", err)
		return nil, err
	}
	bytes, err := ioutil.ReadAll(resp.Body)

	if err != nil {
		fmt.Println("ioutil.ReadAll err=", err)
	}
	fmt.Println("======123", string(bytes))
	respWxApp := &RespWxApp{}
	json.Unmarshal([]byte(string(bytes)), &respWxApp)
	if respWxApp.Errmsg == "" {
		data, err := getUserInfo(respWxApp.AccessToken, respWxApp.Openid)
		if err != nil {
			return nil, err
		}
		respWxApp.Nickname = data.Nickname
		respWxApp.Province = data.Province
		respWxApp.Country = data.Country
		respWxApp.City = data.City
		respWxApp.Headimgurl = data.Headimgurl
		respWxApp.Language = data.Language
		respWxApp.Sex = data.Sex
		respWxApp.AccessToken = ""
	}
	return respWxApp, nil
}

func getUserInfo(accessToken string, openid string) (*RespWxApp, error) {
	uri := "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken + "&openid=" + openid
	resp, err := http.Get(uri)
	if err != nil {
		fmt.Println("这里4=====", err)
		return nil, err
	}
	bytes, err := ioutil.ReadAll(resp.Body)

	if err != nil {
		fmt.Println("ioutil.ReadAll err=", err)
	}
	fmt.Println("======123", string(bytes))
	respWxApp := &RespWxApp{}
	json.Unmarshal([]byte(string(bytes)), &respWxApp)
	return respWxApp, nil
}

type SysWxConfModel struct {
	Id    string `xorm:"pk autoincr" json:"id"`
	Name  string `json:"name"`
	Value string `json:"value"`
}

func Code2Session(code string) (*RespWxSession, error) {
	middlewares.Logger.Println("code====", code)
	uri := "https://api.weixin.qq.com/sns/jscode2session?appid=wxd5cd38899ac167af&secret=2822442afbb71fab05f6fb7a8b43f442&js_code=" + code + "&grant_type=authorization_code"
	resp, err := http.Get(uri)
	if err != nil {
		return nil, err
	}
	bytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		middlewares.Logger.Println("ioutil.ReadAll err=", err)
	}
	middlewares.Logger.Println("resulttttt", string(bytes))
	respWxSession := &RespWxSession{}
	json.Unmarshal([]byte(string(bytes)), &respWxSession)
	if respWxSession.Errmsg != "" {
		middlewares.Logger.Println("Errmsg=", respWxSession.Errmsg)
		return nil, fmt.Errorf(respWxSession.Errmsg)
	}
	fmt.Println("SessionKey======", respWxSession.SessionKey)
	fmt.Println("Unionid======", respWxSession.Unionid)
	fmt.Print(err)
	return respWxSession, nil
}
